package com.jishuo.nontax.api.socket;

import com.jishuo.base.config.NonTaxConfig;
import com.jishuo.base.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

@Slf4j
public class SocketClientCore {

    public String Client(String content) throws IOException {
        String result = null;
        try {
            NonTaxConfig nonTaxConfig = (NonTaxConfig) SpringContextUtil.getBean("nonTaxConfig");

            // 1.建立客户端socket连接，指定服务器位置及端口
            Socket client = new Socket();
            SocketAddress socketAddress = new InetSocketAddress(nonTaxConfig.Scoket_Client_CoreIp,nonTaxConfig.Scoket_Client_CorePort);
            client.connect(socketAddress, 3000);
            client.setSoTimeout(nonTaxConfig.API_Socket_TimeOut);
            // 2.得到socket读写流
            OutputStream os = client.getOutputStream();
            PrintWriter pw = new PrintWriter(os);

            // 3.利用流按照一定的操作，对socket进行读写操作
            // 发送消息
            String sendInfo = content;
            pw.write(sendInfo);
            pw.flush();
            client.shutdownOutput();

            // 输入流
            // 接收到返回消息
            InputStream is = client.getInputStream();
            BufferedReader input = new BufferedReader(new InputStreamReader(is));

            String root="";
            Boolean isend = true;
            log.info("开始接受消息");
            char[] target = new char[1024];
            input.read(target);
            while(isend){
                log.info("接受消息--"+new String(target));
                if(new String(target)!=null){
                    root=root+new String(target).trim();
                }
                if(root.toString().trim().length()>7){
                    // 判断接收完毕
                    if(root.indexOf("</root>")>0){
                        // 截取前4位的无符号整数
                        root=root.substring(4, root.indexOf("</root>")+7);
                        log.info("接收到的消息+++"+root);
                        Document document = DocumentHelper.parseText(root.toString());
                        Element rootDocument =document.getRootElement();//获取根结点
                        Element output=rootDocument.element("output");//获取子结点
                        Element fund=output.element("fund");//获取子子结点
                        result = fund.getTextTrim();

                        isend=false;
                    }else{
                        input.read(target);
                    }
                }else{
                    input.read(target);
                }
            }



            // 4.关闭资源
            input.close();
            is.close();
            pw.close();
            os.close();
            client.close();
            return result;
        } catch (UnknownHostException e) {
            log.info("socket错误："+e.toString());
            return null;
//            e.printStackTrace();
        } catch (IOException e) {
            log.info("socket错误："+e.toString());
            return null;
//            e.printStackTrace();
        } catch (DocumentException e) {
            return null;
//            e.printStackTrace();
        }
    }

}
